/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author patricksamson236
 */
public class StatsCompletes extends JPanel {

    private Fenetre fenetre;
    private double[] tabMontant;
    private int[] tabNbeEntrees;
    private JButton btnRetour;
    private JPanel pnlStats;
    private JLabel lblHead;
    private JScrollPane jsp;
    private GridBagLayout gbl = new GridBagLayout();
    private GridBagConstraints gbc = new GridBagConstraints();
    Font f = new Font("Times New Roman", Font.BOLD, 24);

    public StatsCompletes(Fenetre fenetre) {
        this.fenetre = fenetre;
        this.setLayout(gbl);

        btnRetour = new JButton("Retour");
        btnRetour.addActionListener(new GererEvenements());

        createList();
        buildPanel();

        //on finalise
        this.fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        fenetre.setVisible(true);
    }

    private void buildPanel() {
        pnlStats = new JPanel(gbl);
        JLabel[] tab = new JLabel[50];

        //add the header
        lblHead = new JLabel("Statistiques de tous les clients");
        lblHead.setFont(f);
        gbc.gridwidth = gbc.REMAINDER;
        gbc.weightx = 1;
        gbc.fill = gbc.NONE;
        gbl.setConstraints(lblHead, gbc);
        this.add(lblHead);
        makeSeparator(this);

        //building the panel...
        for (int i = 0; i < tabMontant.length; i++) {
            if (tabNbeEntrees[i] != 0) {
                tab[i] = new JLabel(fenetre.arClient.get(i).getCode()
                        + " --- " + fenetre.arClient.get(i).getNom()
                        + " = " + tabMontant[i] + " $"
                        + " dûs sur " + tabNbeEntrees[i] + " entrée(s)");
                //settings du border
                gbc.gridwidth = gbc.RELATIVE;
                makeBorder(pnlStats);
                //settings du label
                gbc.gridwidth = gbc.REMAINDER;
                gbc.weightx = 1.0;
                gbc.anchor = gbc.WEST;
                gbc.fill = gbc.BOTH;
                gbl.setConstraints(tab[i], gbc);
                pnlStats.add(tab[i]);
            }
        }

        //create the scrollpane and add it to main panel
        makeBorder(this);
        makeBorder(this);
        jsp = new JScrollPane();
        jsp.setViewportView(pnlStats);
        gbc.gridwidth = gbc.RELATIVE;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.anchor = gbc.CENTER;
        gbc.fill = gbc.BOTH;
        gbl.setConstraints(jsp, gbc);
        this.add(jsp);
        makeBorder(this);

        //add the "back" button
        makeSeparator(this);
        makeSeparator(this);
        makeBorder(this);
        gbc.gridwidth = gbc.REMAINDER;
        gbc.gridheight = gbc.REMAINDER;
        gbc.anchor = gbc.FIRST_LINE_START;
        gbc.fill = gbc.NONE;
        gbc.weightx = 1;
        gbl.setConstraints(btnRetour, gbc);
        this.add(btnRetour);
    }

    private void createList() {
        tabMontant = new double[fenetre.arClient.size()];
        tabNbeEntrees = new int[fenetre.arClient.size()];

        //passe a travers toute les liste d'entrées
        for (int i = 0; i < fenetre.arEntree.size(); i++) {
            if (!fenetre.arEntree.get(i).getFacturee()) {
                //on ajoute le montant de l'entrée
                tabMontant[fenetre.arClient.indexOf(fenetre.arEntree.get(i).getClient())] += fenetre.arEntree.get(i).getCout();
                //on incrémente le nbe d'entrées de 1
                tabNbeEntrees[fenetre.arClient.indexOf(fenetre.arEntree.get(i).getClient())]++;

            }
        }
    }

    private void makeSeparator(JPanel pnl) {
        JLabel sep = new JLabel(" ");
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(sep, gbc);
        pnl.add(sep);
        gbc.gridwidth = gbc.CENTER;
        gbc.weighty = 0;
    }

    private void makeBorder(JPanel pnl) {
        gbc.weightx = 0.0;
        JLabel sep = new JLabel("   ");
        gbl.setConstraints(sep, gbc);
        pnl.add(sep);
        gbc.gridwidth = gbc.CENTER;
    }

    class GererEvenements implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnRetour) {
                try {
                    fenetre.AfficherEcranRecherche();
                } catch (IOException ex) {
                    Logger.getLogger(StatsCompletes.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(StatsCompletes.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
